import org.springframework.beans.factory.BeanFactoryAware
import org.springframework.beans.factory.BeanFactory
import org.springframework.beans.BeansException

class PingService implements BeanFactoryAware {
    boolean transactional = false

    def taskExecutor
    def beanFactory

    void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory
    }
    
    def ping(params) {
        def url = params[1]
        def resource = Resource.findByUrl(url)
        if (resource) {
            return pingResource(resource)
        } else {
            println "can't find resource ${url}"
            return "error: url '${url}' not found"
        }
    }

    def pingResource(resource) {
        def fetchFeedProcess = beanFactory.getBean('fetchFeedProcess')
        fetchFeedProcess.resource = resource
        taskExecutor.execute(fetchFeedProcess)
        return "success"
    }
}
